from datetime import datetime
from sqlalchemy import Column, String, Text, DateTime, Integer
from server.db.base import Base

class Memory(Base):
    """长期记忆体存储模型"""
    __tablename__ = "marsx_memories"

    # 自增ID
    id = Column(Integer, autoincrement=True, primary_key=True, index=True)
    user_id = Column(String(64), comment="用户ID", index=True)
    workspace_id = Column(String(64), comment="业务空间ID")
    memory_id = Column(String(64), comment="记忆体ID", index=True)
    description = Column(String(50), comment="记忆体描述")
    memory_node_id = Column(String(64), comment="记忆片段ID", index=True)
    content = Column(Text, comment="记忆片段内容")
    next_token = Column(String(64), comment="查询凭证（Token）") # 获取记忆体列表要用的。取值为上一次 API 调用返回的 NextToken 参数值。
    request_type = Column(Integer, comment="请求类型") # 0: 创建记忆体, 1: 创建记忆片段, 2: 更新记忆片段, 3: 删除记忆片段, 4: 查询记忆片段, 5: 查询记忆体, 6: 查询记忆体列表, 7: 删除记忆体, 8: 更新记忆体, 9: 与智能体对话
    request_id = Column(String(64), comment="阿里云请求ID")
    create_time = Column(DateTime, default=datetime.now, comment="创建时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")

    def __repr__(self):
        return f"<Memory(id='{self.id}', user_id='{self.user_id}', workspace_id='{self.workspace_id}', memory_id='{self.memory_id}', description='{self.description}', memory_node_id='{self.memory_node_id}', content='{self.content}', next_token='{self.next_token}', request_type='{self.request_type}', request_id='{self.request_id}', create_time='{self.create_time}', update_time='{self.update_time}')>"

class MemoryNode(Base):
    __tablename__ = "marsx_memory_nodes"

    id = Column(Integer, autoincrement=True, primary_key=True, index=True)
    user_id = Column(String(64), comment="用户ID", index=True)
    workspace_id = Column(String(64), comment="业务空间ID")
    memory_id = Column(String(64), comment="记忆体ID", index=True)
    memory_node_id = Column(String(64), comment="记忆片段ID", index=True)
    content = Column(Text, comment="记忆片段内容")
    next_token = Column(String(64), comment="查询凭证（Token）") # 获取记忆体列表要用的。取值为上一次 API 调用返回的 NextToken 参数值。
    request_type = Column(Integer, comment="请求类型", default=0) # 0: 创建记忆体, 1: 创建记忆片段, 2: 更新记忆片段, 3: 删除记忆片段, 4: 查询记忆片段, 5: 查询记忆体, 6: 查询记忆体列表, 7: 删除记忆体, 8: 更新记忆体, 9: 与智能体对话
    request_id = Column(String(64), comment="阿里云请求ID")
    create_time = Column(DateTime, default=datetime.now, comment="创建时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")

    def __repr__(self):
        return f"<MemoryNode(id='{self.id}', user_id='{self.user_id}', workspace_id='{self.workspace_id}', memory_id='{self.memory_id}', memory_node_id='{self.memory_node_id}', content='{self.content}', next_token='{self.next_token}', request_type='{self.request_type}', request_id='{self.request_id}', create_time='{self.create_time}', update_time='{self.update_time}')>"

class DeleteMemoryNodeRecord(Base):
    __tablename__ = "marsx_delete_memory_node_records"

    id = Column(Integer, autoincrement=True, primary_key=True, index=True)
    user_id = Column(String(64), comment="用户ID", index=True)
    workspace_id = Column(String(64), comment="业务空间ID")
    memory_id = Column(String(64), comment="记忆体ID", index=True)
    memory_node_id = Column(String(64), comment="记忆片段ID", index=True)
    request_id = Column(String(64), comment="阿里云请求ID")
    request_type = Column(Integer, comment="请求类型", default=0) # 0: 创建记忆体, 1: 创建记忆片段, 2: 更新记忆片段, 3: 删除记忆片段, 4: 查询记忆片段, 5: 查询记忆体, 6: 查询记忆体列表, 7: 删除记忆体, 8: 更新记忆体, 9: 与智能体对话
    deleted_content_type = Column(String(255), comment="删除内容类型")
    create_time = Column(DateTime, default=datetime.now, comment="创建时间")

    def __repr__(self):
        return f"<DeleteMemoryNodeRecord(id='{self.id}', user_id='{self.user_id}', workspace_id='{self.workspace_id}', memory_id='{self.memory_id}', memory_node_id='{self.memory_node_id}', create_time='{self.create_time}')>"
